package com.nice.app.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.format.annotation.DateTimeFormat.ISO;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.nice.app.controller.jqgrid.UserResponse;
import com.nice.app.domain.User;
import com.nice.app.service.UserService;
import com.nice.app.util.NiceFormat;


@Controller
public class UserController {
	
	@Autowired
	private UserService userService;
	
	@RequestMapping(value = "/insertUser")
	public String insertUser() {
		
		User user = new User();
		user.setBirthday(new Date());
		user.setCreationDate(new Date());
		user.setFirstName("Ray-"+(new Date()).hashCode());
		user.setGender("male");
		user.setLastName("Fu");
		user.setMail("123@126.com");
		
		userService.insertUser(user);
		
        return "home";
    }
	
	@RequestMapping(value = "/findUser")
	public String findUser(Model model) {
		
		List<User> list = userService.findUser();
		
		model.addAttribute("userList", list);
		
        return "home";
    }

	
	@RequestMapping(value = "/insertUser2")
	public String insertUser2(Model model) {
		
		User user = new User();
		user.setBirthday(new Date());
		user.setCreationDate(new Date());
		user.setFirstName("Ray-"+(new Date()).hashCode());
		user.setGender("male");
		user.setLastName("Fu");
		user.setMail("123@126.com");
		
		userService.insertUser(user);
		
		List<User> list = userService.findUser();
		
		model.addAttribute("userList", list);
		
        return "nice";
    }
	
	@RequestMapping(value = "/findAll", method = RequestMethod.GET)
	
    public @ResponseBody @DateTimeFormat(iso=ISO.DATE_TIME) UserResponse findAll() {

	
     List<User> users = userService.findUser();
      
     UserResponse response = new UserResponse();
      
     response.setRows(users);

     response.setRecords( String.valueOf(users.size()) );
      
     response.setPage( "1" );
      
     response.setTotal( "10" );
      
     return response;
 }
	
	@RequestMapping(value = "/userPage")
	public String userPage(Model model) {
		
        return "user/user";
    }
	
	@RequestMapping(value = "/editUserPage")
	public @ResponseBody String editUserPage(Model model) {
 
		
        return "ewwl";
    }
	
	@RequestMapping(value = "/editUser")
	public @ResponseBody String editUser(final HttpServletRequest request) throws ParseException{
		
		User user = new User();
		user.setBirthday((new SimpleDateFormat(NiceFormat.shortDate)).parse((String)request.getParameter("birthday")));		
		user.setFirstName((String)request.getParameter("firstName"));
		user.setGender((String)request.getParameter("gender"));
		user.setLastName((String)request.getParameter("lastName"));
		user.setMail((String)request.getParameter("mail"));
		
		String operation = (String)request.getParameter("oper");
		if("add".equals(operation)){
			user.setCreationDate(new Date());
			userService.insertUser(user);
		}else if("edit".equals(operation)){
			user.setUserId(Long.valueOf((String)request.getParameter("id")));
			userService.updateUser(user);
		}
		
		return null;
		
	}
	
	@RequestMapping(value = "/deleteUser")
	public @ResponseBody String deleteUser(final HttpServletRequest request){
		Object userId = request.getParameter("id");
		if(userId == null){
			return null;
		}else{
			userService.deleteUser(Long.valueOf((String)userId));
//			userService.deleteUserByIdList((String)userId);
		}
		
		return null;
		
	}
	
	
	
	
}
